package com.vis.restfs;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.presigner.S3Presigner;
import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest;
import software.amazon.awssdk.services.s3.presigner.model.PresignedGetObjectRequest;

import java.net.URI;
import java.time.Duration;

public class PreDownload {

    public static void main(String[] args) {
        S3Presigner presigner = S3Presigner.builder()
                .endpointOverride(URI.create("http://39.103.63.7:9000"))
                .region(Region.US_EAST_1)
                .credentialsProvider(
                        StaticCredentialsProvider.create(
                                AwsBasicCredentials.create("rustfsadmin", "rustfsadmin")
                        )
                )
                .build();

        GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                .bucket("my-bucket")
                .key("hello.txt")
                .build();

        GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder()
                .getObjectRequest(getObjectRequest)
                .signatureDuration(Duration.ofMinutes(15)) // 有效期 15 分钟
                .build();

        PresignedGetObjectRequest presignedRequest = presigner.presignGetObject(presignRequest);

        System.out.println("Presigned URL: " + presignedRequest.url());
    }
}